package com.moyu.plugin.cat

import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.ClassContext
import com.android.build.api.instrumentation.ClassData
import com.android.build.api.instrumentation.InstrumentationParameters
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.objectweb.asm.ClassVisitor

abstract class CatClassVisitorFactory : AsmClassVisitorFactory<CatClassVisitorFactory.TrackParam> {

    interface TrackParam : InstrumentationParameters {
        @get:Input
        val catOn: Property<Boolean>

        @get:Input
        val catName: Property<String>
    }

    override fun isInstrumentable(classData: ClassData): Boolean {
        val targetName = classData.className
        val catName = parameters.orNull?.catName?.get() ?: "is not init"
        val match = classData.className.contains("MyActivity")
        if (match) {
            println("name=$targetName catName=$catName")
        }
        return match
        //return true
    }


    override fun createClassVisitor(
        classContext: ClassContext, nextClassVisitor: ClassVisitor
    ): ClassVisitor {
        val catOn = parameters.orNull?.catOn?.get() ?: false
        val api = instrumentationContext.apiVersion.get()
        return CatClassVisitor(api, catOn, nextClassVisitor)
    }
}